﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace PolePosition.v3.Desktop.Selectors.Controls
{
    public partial class Projects : DialogEditBase
    {
        public EventHandler<ItemCheckedEventArgs> ProjectChecked;

        public Projects()
        {
            InitializeComponent();
            BindLists();
            listSelectorProjects.ItemChecked += ItemChecked;
        }

        private void ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            ProjectChecked(this, e);
        }

        private void Projects_Load(object sender, EventArgs e)
        {
            
        }

        private bool m_BindComplete = false;
        private List<EntityLibrary.ProjectEntity> m_AfterBindingCompleteSelectedProjects = null;

        private bool m_AllowMultiSelect = false;
        [DefaultValue(false)]
        public bool AllowMultiSelect 
        { 
            get
            {
                return m_AllowMultiSelect;   
            }
            set
            {
                m_AllowMultiSelect = value;
                if (m_AllowMultiSelect)
                    base.Subtitle = "Select One or More Projects";
                else
                    base.Subtitle = "Select Project";

                listSelectorProjects.AllowMultiSelect = m_AllowMultiSelect;

                //listViewExProjects.MultiSelect = m_AllowMultiSelect;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedProjectList
        {
            get { return listSelectorProjects.GetSelectedValueList<int>(); }
            set 
            { 
                listSelectorProjects.SetSelectedValueList<int>(value);
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<EntityLibrary.ProjectEntity> SelectedProjects
        {
            get
            {
                return listSelectorProjects.GetSelectedItems<EntityLibrary.ProjectEntity>();
            }
            set
            {
                if (!m_BindComplete)
                {
                    m_AfterBindingCompleteSelectedProjects = value;
                    return;
                }
                
                if (value != null)
                    listSelectorProjects.SetSelectedItems<EntityLibrary.ProjectEntity>(value);
                
            }
        }

        private void BindLists()
        {
            //Load the project List
            if (Program.LoginResult != null)
            {
                listSelectorProjects.DisplayMember = EntityLibrary.ProjectEnumerations.Columns.Name.ToString();
                listSelectorProjects.ValueMember = EntityLibrary.ProjectEnumerations.Columns.Vendor_ProjectId.ToString();

                if (Program.LoginResult.LoginUserType == EntityLibrary.LoginEntity.LoginUserTypes.PPI && Program.UserSelections.UtilityId != 0)
                {
                    var _Projects = from project in Program.LoginResult.Projects
                                    where project.UtilityFk == Program.UserSelections.UtilityId
                                    select project;

                    if (_Projects == null)
                        listSelectorProjects.DataSource = null;
                    else
                        listSelectorProjects.DataSource = _Projects.ToList();
                }
                else
                {
                    listSelectorProjects.DataSource = Program.LoginResult.Projects;
                }
                listSelectorProjects.SelectNone();
            
                m_BindComplete = true;

                //if selected projects were set prior to the binding of the list then do so now 
                if (m_AfterBindingCompleteSelectedProjects != null)
                    SelectedProjects = m_AfterBindingCompleteSelectedProjects;
            }
        }


    }
}
